package cn.xz.zysjz;


import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentTransaction;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import cn.xz.zysjz.base.BaseActivity;
import cn.xz.zysjz.bean.CommonLib;
import cn.xz.zysjz.bean.MessageEvent;
import cn.xz.zysjz.databinding.ActivityMainBinding;
import cn.xz.zysjz.fragment.ActivityFragment;
import cn.xz.zysjz.fragment.HomeFragment;
import cn.xz.zysjz.fragment.MineFragment;
import cn.xz.zysjz.utils.AppHelper;

public class MainActivity extends BaseActivity {

    //绑定xml布局文件-也就是手机上看到的界面
    ActivityMainBinding mBinding;
    //首页模块
    HomeFragment homeFragment;
    //大厅模块
    ActivityFragment activityFragment;
    //我的模块
    MineFragment mineFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        //订阅消息
        EventBus.getDefault().register(MainActivity.this);
        //默认显示首页模块
        selectTab(0);
        //控件点击事件
        initEvent();
    }

    private void initEvent(){
        //点击首页
        mBinding.llcHome.setOnClickListener(v->{
            selectTab(0);
        });
        //点击大厅
        mBinding.llcActivity.setOnClickListener(v->{
            selectTab(1);
        });
        //点击我的
        mBinding.llcMine.setOnClickListener(v->{
            selectTab(2);
        });
    }

    //切换不同的模块
    private void selectTab(int i){
        FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction();
        hideFragment(transaction);
        int mC = Color.parseColor("#00D4E2");
        int mNC = Color.parseColor("#000000");
        mBinding.tvHome.setTextColor(mNC);
        mBinding.tvActivity.setTextColor(mNC);
        mBinding.tvMine.setTextColor(mNC);
        mBinding.ivHome.setImageDrawable(AppHelper.getDrawable(R.mipmap.icon_home1));
        mBinding.ivActivity.setImageDrawable(AppHelper.getDrawable(R.mipmap.icon_a1));
        mBinding.ivMine.setImageDrawable(AppHelper.getDrawable(R.mipmap.icon_mine1));
        switch (i){
            case 0:
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.fl_layout, homeFragment);
                } else {
                    transaction.show(homeFragment);
                }
                mBinding.tvHome.setTextColor(mC);
                mBinding.ivHome.setImageDrawable(AppHelper.getDrawable(R.mipmap.icon_home));
                break;
            case 1:
                if (activityFragment == null) {
                    activityFragment = new ActivityFragment();
                    transaction.add(R.id.fl_layout, activityFragment);
                } else {
                    transaction.show(activityFragment);
                }
                mBinding.tvActivity.setTextColor(mC);
                mBinding.ivActivity.setImageDrawable(AppHelper.getDrawable(R.mipmap.icon_a));
                break;
            case 2:
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                    transaction.add(R.id.fl_layout, mineFragment);
                } else {
                    transaction.show(mineFragment);
                }
                mBinding.tvMine.setTextColor(mC);
                mBinding.ivMine.setImageDrawable(AppHelper.getDrawable(R.mipmap.icon_mine));
                break;
            default:
                break;
        }
        transaction.commit();
    }


    //隐藏所有的Fragment
    private void hideFragment(FragmentTransaction transaction){
        if (homeFragment != null){
            transaction.hide(homeFragment);
        }
        if (activityFragment != null){
            transaction.hide(activityFragment);
        }
        if (mineFragment != null){
            transaction.hide(mineFragment);
        }
    }

    @Subscribe
    public void onSubscriberEvent(MessageEvent message) {
        if (message.getMessage().contains("切换")){
            //切换
            selectTab(1);
        }
        if (message.getMessage().contains("关闭")){
            //关闭APP
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}